package zhxt.helmet.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import zhxt.helmet.dao.ResultDao;
import zhxt.helmet.pojo.Result;

import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

@Service("resultService")
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public class ResultService {
    @Autowired
    ResultDao resultDao;
    //获取结果并统计
    public List<List<Integer>> analyse(List<Integer> cameraIdList, Timestamp startTime, Timestamp endTime ){
        //System.out.println("startTime:"+startTime.toLocalDateTime().toLocalDate()+"  endTime:"+endTime.toLocalDateTime().toLocalDate());
        List<List<Integer>> r = new ArrayList<>();
        //获取结果
        List<Result> results = resultDao.selectResults(cameraIdList, startTime, endTime);
        //统计结果
        List<Integer> rt = new ArrayList<>(Collections.nCopies((int)ChronoUnit.DAYS.between(startTime.toInstant(), endTime.toInstant())+1, 0));//以日期为单位
        List<Integer> returnCameraList;//防止统计的顺序和输入顺序不一致
        List<Integer> rc = new ArrayList<>();//以摄像头(地点)为单位
        List<Integer> rh = new ArrayList<>(Collections.nCopies(2,0));//头盔总数: [有头盔总数, 无头盔总数]
        Map<Integer, Integer> countMap = new HashMap<>();//用于以摄像头为单位的统计
        for(Result result : results){
            //以日期为单位
            if(!result.getHaveHelmet() ){
                long daysBetween = ChronoUnit.DAYS.between(startTime.toLocalDateTime().toLocalDate(), result.getLocalDate());
                //System.out.println("当前日期："+result.getLocalDate()+" daysBetween: "+daysBetween);
                rt.set((int) daysBetween, rt.get((int) daysBetween) + 1);
            }
            //以摄像头为单位
            if(!result.getHaveHelmet() ) {
                int cameraId = result.getCameraId();
                countMap.put(cameraId, countMap.getOrDefault(cameraId, 0) + 1);
            }
            //头盔总数
            if(result.getHaveHelmet()) rh.set(0,rh.get(0)+1);
            else rh.set(1, rh.get(1)+1);
        }
        //处理以摄像头为单位的统计
        returnCameraList = new ArrayList<>(countMap.keySet());  // 保存摄像头ID列表
        for (Integer cameraId : returnCameraList) {//保存对应的不带头盔人数 保持顺序一致
            rc.add(countMap.get(cameraId));
        }
        List<Integer> difference = cameraIdList.stream()//获取两个摄像头列表的差集
                .filter(e -> !returnCameraList.contains(e))
                .collect(Collectors.toList());
        for (Integer cameraId : difference) {//补充人数为0的摄像头和对应的人数
            returnCameraList.add(cameraId);
            rc.add(0);
        }
        //返回结果
        r.add(rt);
        r.add(returnCameraList);
        r.add(rc);
        r.add(rh);
        return r;
    }
    //向数据库保存最新数据
    public int insertResult(Result result){
        return resultDao.insertResult(result);
    }
    //批量保存
    public void insertResults(List<Result> results){
        resultDao.insertResults(results);
    }
}
